package com.lagou.edu.controller;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@Controller
@RequestMapping("")
public class LoginController {

    @RequestMapping("/login")
    public String login(HttpServletRequest request,
                        HttpServletResponse response,
                        String username,
                        String password,
                        Model model) throws Exception{
        if (username == null || password == null){
            return "login";
        }
        if ("admin".equals(username)&& "admin".equals(password)){
            //登录成功，设置session，并跳转到主页
            request.getSession().setAttribute("username",username);
            response.sendRedirect("index");
            return "index";
        }
        response.setStatus(401);
        return "login";
    }

    @RequestMapping("/logout")
    public String logout() throws Exception{
        return "login";
    }


    @RequestMapping("/index")
    public String index() throws Exception{
        return "index";
    }
}
